package com.chinaums.study.restful.controller;

import com.chinaums.study.restful.service.RestfulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/pic")
public class PicController {

    private RestfulService restfulService;

    public PicController(@Autowired RestfulService restfulService) {
        this.restfulService = restfulService;
    }

    @PostMapping("/retrieval")
    public List<?> retrieval(@RequestPart("pic") Part pic, final Integer[] repos) throws IOException {
        final Long faceImageId;
        try (final InputStream in = pic.getInputStream()) {
            faceImageId = restfulService.upload(in);
        }
        return restfulService.retrieval(faceImageId, Arrays.stream(repos).filter(Objects::nonNull).collect(Collectors.toList()));
    }

    @GetMapping("/load-repos")
    public List<Map<String, Object>> loadRepos() {
        return restfulService.loadRepos();
    }
}
